<?php
require 'file/SFL.php';
class WordList {
    public function __construct($type) {
        $f=DATA_DIR.'/list/'.$type;
        $this->mSFL=new SFL($f);
    }
    public function add($word) {
        $word=trim($word);
        if (preg_match('/^[a-zA-Z0-9 ]+$/',$word)) {
            $this->mSFL->delete($word);
            $this->mSFL->add($word);
        }
    }
    public function delete($word) {
        $word=trim($word);
        if (preg_match('/^[a-zA-Z0-9 ]+$/',$word)) {
            $this->mSFL->delete($word);
        }
    }
    public function get($num,$offset=0) {
        return $this->mSFL->getLatest($num,$offset);
    }
    public function exist($word) {
        return $this->mSFL->exist($word);
    }
    public function count() {
        return $this->mSFL->count();
    }
}
function get_dict_cache() {
    require_once 'Zend/Cache.php';
    $frontendOptions = array(
       'lifetime' => null, // cache lifetime of 2 hours
       'automatic_serialization' => true
    );

    $backendOptions = array(
        'cache_dir' => '/tmp/' // Directory where to put the cache files
    );

    // getting a Zend_Cache_Core object
    return Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

}
function check_installation() {
    $dirs = array(DATA_DIR,DATA_DIR.'/list',TMP_DIR);
    foreach ($dirs as $dir) {
        if (!is_dir($dir) && !mkdir($dir)) {
            echo $dir,' dose not exist.';
            exit;
        }
        if (!is_writeable($dir)) {
            echo $dir,' is not writeable.';
            exit;
        }
    }
}
function digestPassword($pass) {
    return md5(md5($pass).'dict');
}
function printer($text,$from,$to) {
    $text=htmlspecialchars($text);
    $text=str_replace(array("\t"," "),'&nbsp;',$text);
    $text=str_replace(array("\n","\r"),'<br />',$text);
    echo $text;
}
